package top.hundreds.oasys.service;

import org.springframework.stereotype.Service;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.crud.service.BaseService;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Meeting;
import top.hundreds.oasys.bean.entity.MeetingParticipant;
import top.hundreds.oasys.bean.entity.User;
import top.hundreds.oasys.bean.enums.MeetingStatus;
import top.hundreds.oasys.bean.enums.MeetingType;
import top.hundreds.oasys.bean.vo.MeetingInfoVo;
import top.hundreds.oasys.bean.vo.MeetingVo;
import top.hundreds.oasys.bean.vo.UserVo;
import top.hundreds.oasys.mapper.MeetingMapper;
import top.hundreds.oasys.mapper.MeetingParticipantMapper;
import top.hundreds.oasys.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Meeting)表服务实现类
 *
 * @author lcf
 * @since 2021-01-27 14:59:55
 */
@Service("meetingService")
public class MeetingService extends BaseService<Meeting, MeetingMapper> {


    @Resource
    private Converter<Meeting, MeetingInfoVo> meetingInfoVoBuilder;

    @Resource
    private UserMapper userMapper;

    @Resource
    private MeetingParticipantMapper meetingParticipantMapper;

    public MeetingInfoVo info(Meeting meeting) {
        MeetingInfoVo meetingInfoVo = new MeetingInfoVo();
        meetingInfoVoBuilder.buildVo(meeting, meetingInfoVo);
        List<Long> userIds = meetingParticipantMapper.getUserIdsByMeetingId(meeting.getId());
        List<UserVo> userList = new ArrayList<>(userIds.size());
        Converter<User, UserVo> userUserVoConverter = new Converter<>();
        for (Long userId : userIds) {
            User user = userMapper.selectById(userId);
            UserVo userVo = new UserVo();
            userUserVoConverter.buildVo(user, userVo);
            userList.add(userVo);
        }
        meetingInfoVo.setParticipantList(userList);
        meetingInfoVo.setStatusName(MeetingStatus.formValue(meetingInfoVo.getStatus()).getLabel());
        meetingInfoVo.setTypeName(MeetingType.formValue(meetingInfoVo.getType()).getLabel());
        meetingInfoVo.setUserName(userMapper.selectById(meetingInfoVo.getUserId()).getUsername());
        System.out.println(meetingInfoVo);
        return meetingInfoVo;
    }

    public MeetingVo buildVo(Meeting meeting) {
        MeetingVo meetingVo = new MeetingVo();
        new Converter<Meeting, MeetingVo>().buildVo(meeting, meetingVo);
        meetingVo.setStatusName(MeetingStatus.formValue(meetingVo.getStatus()).getLabel());
        meetingVo.setTypeName(MeetingType.formValue(meetingVo.getType()).getLabel());
        meetingVo.setUserName(userMapper.selectById(meetingVo.getUserId()).getNickname());
        return meetingVo;
    }

    public void add(Meeting meeting, List<UserVo> userList) {
        Meeting record = insert(meeting);
        for (UserVo userVo : userList) {
            meetingParticipantMapper.insert(MeetingParticipant.builder().meetingId(record.getId()).userId(userVo.getId()).build());
        }
    }

    public void edit(Meeting meeting, List<UserVo> userList) {
        Meeting update = update(meeting);
        meetingParticipantMapper.deleteByMeetingId(meeting.getId());
        for (UserVo userVo : userList) {
            meetingParticipantMapper.insert(MeetingParticipant.builder().meetingId(update.getId()).userId(userVo.getId()).build());
        }
    }

    public List<MeetingVo> getPartMeeting(Long id, PageParam<Meeting> pageParam) {
        Integer offset = (pageParam.getPage() - 1) * pageParam.getLimit();
        List<Long> meetingIds = meetingParticipantMapper.getMeetingIdsByUserId(id, offset, pageParam.getLimit());
        List<MeetingVo> meetingVoList = new ArrayList<>(meetingIds.size());
        for (Long meetingId : meetingIds) {
            meetingVoList.add(buildVo(get(meetingId)));
        }
        return meetingVoList;
    }
}
